<?php
/**
 * 系统配置
 *
 * @category     Shuxun Cms
 * @date         2015年10月18日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         SettingsController.php
 */
namespace Scshux\Backend\Controllers;
use Scshux\Mvc\SModel;
use Scshux\Mvc\SQuery;
use Scshux\Utils\Uploader;
class SettingsController extends  ControllerBase
{
	/**
	 * 系统框架页面
	 */
	public  function  indexAction()
	{
		if($this->request->isPost()){
			$postData = $_POST;
			//加入语言
			$_POST['site_id'] = $this->getCore()->getSiteID();

			if($_FILES)
			{
				foreach ($_FILES as $keyname=>$file)
				{
					if($file['error'] ===0){

						$uploader = new Uploader();
						$tempfile = $uploader->upload($file);
						if($tempfile){
							$postData[$keyname] = $tempfile;
						}
					}
				}
			}

			foreach ($postData as $key=>$value)
			{
				$configModel = new SModel('config');
				$configModel->setData(array('value'=>$value));
				$configModel->update("code ='{$key}' and site_id='{$_POST['site_id']}'");
			}
			$this->redirect('settings');
		}
		$this->view->setVar('configgroup', $this->getConfigs());
	}


	/**
	 * 获取配置
	 */
	protected  function getConfigs()
	{
		$configModel = new SModel('config');

		//新增语言过滤
		$site_id = $this->getCore()->getSiteID();
		$where = 'site_id = '.$site_id;

		$items = $configModel->query($where,'*','sort desc,id','ASC');

		$groupArr = array();

		if(count($items) > 1){
			foreach ($items as $item)
			{
				if($item->type == 3){
					$tempvalues = explode(';', $item->values);
					$values = array();
					foreach ($tempvalues as $value)
					{
						$value = explode('|', $value);
						$values[$value[0]] = $value[1];
					}
					$item->values = $values;
				}
				$groupArr[$item->group][] = $item;
			}
		}

		return $groupArr;
	}

}